package drds.server.route.parser.core;

import java.util.ArrayList;
import java.util.List;

import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.sqlobject.expression.binary.BinaryExpression;
import drds.server.sqlparser.table.ShardKeyValue;

/**
 * 
 * 
 * <pre>
 * where user_id=? and name=? and (add_time > ? and add_time<?) and stuats =
 * ? and stuats in (?,?) order by traveldate desc limit 20
 * </pre>
 * 
 * <pre>
 * where语句由很多条件构成
 * abc a.x= and b.() c=()
 * 
 * </pre>
 * 
 * 
 * 
 * 嵌套可继续拆分的BinaryExpression
 */
public class SplitableExpression {
	/**
	 * splitable expression,can go on split with key word 'or'
	 */
	private BinaryExpression binaryExpression;
	/**
	 * splitable expression list,can go on split with key word 'or'
	 */
	private List<Expression> binaryExpressionList = new ArrayList<Expression>();

	public SplitableExpression() {
	}

	/**
	 * @param binaryExpression
	 *            splitable expression
	 */
	public SplitableExpression(BinaryExpression binaryExpression) {
		this.binaryExpression = binaryExpression;
	}

	public void setBinaryExpression(BinaryExpression splitableExpression) {
		this.binaryExpression = splitableExpression;
	}

	public BinaryExpression getBinaryExpression() {
		return binaryExpression;
	}

	/**
	 * @param binaryExpression
	 *            splitable expression
	 */
	public void addBinaryExpression(Expression binaryExpression) {
		this.binaryExpressionList.add(binaryExpression);
	}

	public List<Expression> getBinaryExpressionList() {
		return binaryExpressionList;
	}

	private List<List<ShardKeyValue>> shardKeyValueListList = new ArrayList<List<ShardKeyValue>>();

	/**
	 * whereExpr并不是一个where的全部，有部分条件在outConditions
	 */
	private List<ShardKeyValue> outSideShardKeyValueList = new ArrayList<ShardKeyValue>();

	/**
	 * <pre>
	 * or ( a or b or c) or  (d or e or f) or (g and h )
	 * </pre>
	 */
	private List<SplitableExpression> innerSplitableExpressionList = new ArrayList<SplitableExpression>();

	private boolean finishedParse = false;

	public List<ShardKeyValue> getOutSideShardKeyValueList() {
		return outSideShardKeyValueList;
	}

	public void addOutSideShardKeyValueList(List<ShardKeyValue> shardKeyValueList) {
		this.outSideShardKeyValueList.addAll(shardKeyValueList);
	}

	public boolean isFinishedParse() {
		return finishedParse;
	}

	public void setFinishedParse(boolean finishedParse) {
		this.finishedParse = finishedParse;
	}

	public List<List<ShardKeyValue>> getShardKeyValueListList() {
		return shardKeyValueListList;
	}

	public void setShardKeyValueListList(List<List<ShardKeyValue>> shardKeyValueListList) {
		this.shardKeyValueListList = shardKeyValueListList;
	}

	public void addInnerSplitableExpression(SplitableExpression splitableExpression) {
		this.innerSplitableExpressionList.add(splitableExpression);
	}

	public List<SplitableExpression> getInnerSplitableExpressionList() {
		return this.innerSplitableExpressionList;
	}
}
